package com.lgy.filter;

import com.lgy.common.SessionUtils;
import com.lgy.model.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class LoginFilter implements Filter {
    @Autowired
    private Environment env;

    //不需要过滤的URL
    private static final String[] URLS = {"login.html","sys/login","sys/getLoginInfo","sys/logout",".js",".css",".ico",".jpg",".png"};
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String requestPath = httpRequest.getRequestURL().toString();
        System.out.println("RequestURL=" + requestPath);
        //登录|静态页面直接放行
        for(String url : URLS) {
            if(requestPath.contains(url)) {
                chain.doFilter(request, response);
                return;
            }
        }
        //如果是Camunda服务就放行
        String camundaSwitch = env.getProperty("camunda.switch", "false");
        if(Boolean.valueOf(camundaSwitch)) {
            chain.doFilter(request, response);
            return;
        }

        //判断session是否为NULL
        SysUser sysUser = (SysUser) SessionUtils.getAttr(httpRequest, SessionUtils.SESSION_USER);;
        if(null != sysUser) {
            System.out.println("登录的用户：" + sysUser.getUserName());
            chain.doFilter(request, response);
        } else {
            httpResponse.sendRedirect("/login.html");
        }
    }

    /*private Map<String, String> parseURLParams(String query) {
        Map<String, String> params = new HashMap<>();

        try {
            if (query != null) {
                String[] paramPairs = query.split("&");
                for (String pair : paramPairs) {
                    String[] keyValue = pair.split("=");
                    //String key = URLDecoder.decode(keyValue[0], "UTF-8");
                    //String value = URLDecoder.decode(keyValue[1], "UTF-8");
                    String key = keyValue[0];
                    String value = keyValue[1];
                    params.put(key, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return params;
    }

    private String unescape(String src) {
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length());
        int lastPos = 0, pos = 0;
        char ch;
        while (lastPos < src.length()) {
            pos = src.indexOf("%", lastPos);
            if (pos == lastPos) {
                if (src.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(src
                            .substring(pos + 2, pos + 6), 16);
                    tmp.append(ch);
                    lastPos = pos + 6;
                } else {
                    ch = (char) Integer.parseInt(src
                            .substring(pos + 1, pos + 3), 16);
                    tmp.append(ch);
                    lastPos = pos + 3;
                }
            } else {
                if (pos == -1) {
                    tmp.append(src.substring(lastPos));
                    lastPos = src.length();
                } else {
                    tmp.append(src.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }
        return tmp.toString();
    }*/
}
